package reflect;
/**
 * 反射机制调用无参方法
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * 反射机制调用方法
 */
public class ReflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //实例化
//        Class cls = Class.forName("reflect.Person");
//        Object obj = cls.newInstance();//new Person();
//        //通过类对象获取对应的方法
//        Method method = cls.getMethod("satHello"); 下面这两句是重点 就是用他俩来调用方法的
//        method.invoke(obj);

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入类名：");
        String classname = scanner.next();
        System.out.println("请输入方法名：");

        String methodname = scanner.next();
        Class cls = Class.forName(classname);
        Object obj = cls.newInstance();
        Method method = cls.getMethod(methodname);
        method.invoke(obj);


    }





}
